package com.fjwt.gz.proxy.ctrl.award.virtual;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardVirtualCdkDTO;
import com.fjwt.gz.db.vo.AwardVirtualCdkVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardVirtualCdkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 虚拟奖品CDK管理
 *
 * @Author: 梅亚锋
 * @Date: 2025-02-25
 */
@RestController
@Slf4j
@RequestMapping("/api/virtualAward/cdk")
public class VirtualAwardCdkController extends CommonCtrl {

    @Autowired
    private AwardVirtualCdkService awardVirtualCdkService;

    /**
     * cdk列表
     * @return
     */
    @GetMapping
    public ApiRes list() {
        AwardVirtualCdkDTO awardCdkDTO = getObject(AwardVirtualCdkDTO.class);
        String agentNo = getCurrentUser().getAgentNo();
        awardCdkDTO.setAgentNo(checkAgentAuth(agentNo, awardCdkDTO.getAgentNo()));
        log.info("查询虚拟奖品CDK列表，请求地址：/api/virtualAward/cdk，请求参数：{}", JSONUtil.toJsonStr(awardCdkDTO));
        IPage<AwardVirtualCdkVO> page = awardVirtualCdkService.queryList(getIPage(true), awardCdkDTO);
        log.info("查询虚拟奖品CDK列表，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 查询已划拨的列表
     * @return
     */
    @PostMapping("/queryUsedCdkList")
    public ApiRes queryUsedCdkList() {
        AwardVirtualCdkDTO awardCdkDTO = getObject(AwardVirtualCdkDTO.class);
        String agentNo = getCurrentUser().getAgentNo();
        awardCdkDTO.setAgentNo(checkAgentAuth(agentNo, awardCdkDTO.getAgentNo()));
        log.info("pc端内部商品查询已划拨的列表，请求地址：/api/virtualGoods/inside/queryUsedCdkList，请求参数：{}", JSONUtil.toJsonStr(awardCdkDTO));
        IPage<AwardVirtualCdkVO> page = awardVirtualCdkService.queryUsedCdkList(getIPage(true), awardCdkDTO);
        log.info("pc端内部商品查询已划拨的列表，请求地址：/api/virtualGoods/inside/queryUsedCdkList，返回结果：{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }


    /**
     * 导入cdk数据
     * @return
     */
    //    @PreAuthorize("hasAnyAuthority('ENT_VIRTUAL_GOODS_CDK_IMPORT')")
    @PostMapping("/importCdk")
    public ApiRes importCdk() {
        AwardVirtualCdkDTO awardCdkDTO = getObject(AwardVirtualCdkDTO.class);
        log.info("导入虚拟奖品CDK数据，请求地址：/api/virtualAward/cdk/importCdk，请求参数：{}", JSONUtil.toJsonStr(awardCdkDTO));
        awardCdkDTO.setCreatedUid(getCurrentUser().getSysUserId());
        String errFileUrl = awardVirtualCdkService.importCdk(awardCdkDTO, OssFileConfig.BIZ_TYPE.UPLOAD);
        log.info("导入虚拟奖品CDK数据，返回导入失败文件url：{}", errFileUrl);
        return ApiRes.ok(errFileUrl);
    }


}
